//
//  UserDefault+Extension.swift
//  GreenLight
//
//  Created by ZWJY on 2022/8/22.
//

import Foundation

public extension UserDefaults {

    /// 存值
    class func userDetaultsSetValue(value: Any?, key: String?) {
        guard let value = value,
                let key = key else { return }
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }

    /// 取值
    class func userDefaultsGetValue(key: String?) -> Any? {
        guard let key = key else {
            return nil
        }

        return UserDefaults.standard.value(forKey: key)
    }

    /// 删除某个值
    class func remove(key: String?) {
        guard let key = key,
        let _ = UserDefaults.standard.value(forKey: key) else {
            return
        }

        UserDefaults.standard.removeObject(forKey: key)
    }

    /// 删除所有
    class func removeAll() {
        guard let id = Bundle.main.bundleIdentifier else { return }
        UserDefaults.standard.removePersistentDomain(forName: id)
    }
}
